package net.dasviertel.buddylockscreen.views;

import android.graphics.Paint;
import android.view.MotionEvent;
import android.graphics.Bitmap;

import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.content.Context;
import net.dasviertel.buddylockscreen.application.AppPreferences;
import net.dasviertel.buddylockscreen.data.Calculator;

public class CreateImageView extends View {

	private static final int INITIAL_TEXTSIZE = 60;
	private static final int INITIAL_XPOS_NAME = 45;
	private static final int INITIAL_YPOS_NAME = 150;

	private Paint infoTextPaint;
	private String info = "";
	private int infoXPos, infoYPos;
	private int width, height;

	private int infoTextWidth, infoTextHeight;
	private float touchX, touchY;
	private Bitmap bitmap, outputBitmap;

	private Canvas outputCanvas;

	private Rect infoRect;

	public CreateImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public CreateImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public CreateImageView(Context context) {
		super(context);
	}

	public void init() {

		infoTextPaint = new Paint();
		infoTextPaint.setColor(Color.BLACK);
		infoTextPaint.setStyle(Paint.Style.FILL);
		infoTextPaint.setAntiAlias(true);
		infoTextPaint.setTextSize(INITIAL_TEXTSIZE);

		setInfo(AppPreferences.getInstance().getContactPhoneNumber());

	}

	public Bitmap getOutputBitmap() {

		Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
		outputBitmap = Bitmap.createBitmap(width, height, conf);

		return outputBitmap;
	}

	public void setOutputBitmap(Bitmap outputBitmap) {
		this.outputBitmap = outputBitmap;
	}

	public Canvas getOutputCanvas() {
		if (this.outputCanvas == null) {
			this.outputCanvas = new Canvas(getOutputBitmap());
		}
		return outputCanvas;
	}

	public void setOutputCanvas(Canvas outputCanvas) {
		this.outputCanvas = outputCanvas;
	}

	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		outputCanvas = getOutputCanvas();

		canvas.drawBitmap(bitmap, 0, 0, null);
		outputCanvas.drawBitmap(bitmap, 0, 0, null);

		canvas.drawText(getName(), getInfoXPos(), getInfoYPos(), infoTextPaint);
		outputCanvas.drawText(getName(), getInfoXPos(), getInfoYPos(), infoTextPaint);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		int action = event.getAction() & MotionEvent.ACTION_MASK;
		switch (action) {

		case MotionEvent.ACTION_DOWN: {
			break;
		}

		case MotionEvent.ACTION_MOVE: {

			touchX = event.getX();
			touchY = event.getY();

			if (isTouched((int) touchX, (int) touchY, getInfoRect())) {
				setInfoXPos((int) event.getX() - infoTextWidth / 2);
				setInfoYPos((int) event.getY());
			}
			invalidate();
			break;
		}
		case MotionEvent.ACTION_UP: {

			if (isTouched((int) touchX, (int) touchY, infoRect)) {
				setInfoXPos((int) event.getX() - infoTextWidth / 2);
				setInfoYPos((int) event.getY());
			}
			invalidate();
			break;
		}
		}

		return true;
	}

	public Rect getCurrentInfoRect() {

		infoRect = getInfoRect();

		infoTextPaint.getTextBounds(getName(), 0, getName().length(), infoRect);

		infoTextHeight = Math.abs(Math.abs(infoRect.bottom) - Math.abs(infoRect.top));
		infoTextWidth = Math.abs(Math.abs(infoRect.right) - Math.abs(infoRect.left));

		infoRect.top = getInfoYPos() - infoTextHeight;
		infoRect.bottom = (int) (getInfoYPos() + infoTextPaint.getTextSize());
		infoRect.left = getInfoXPos();
		infoRect.right = getInfoXPos() + infoTextWidth;

		return infoRect;
	}

	public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

		this.width = getMeasuredWidth();
		this.height = getMeasuredHeight();

		Calculator.getInstance();

		bitmap = Calculator.decodeSampledBitmapFromFile(AppPreferences.getInstance().getImageNameAndPath(), width, height);
	}

	public void recycleBitmap(Bitmap bitmap) {

		if (bitmap != null) {
			bitmap.recycle();
			bitmap = null;
		}
	}

	public void recycleShownBitmap() {

		// recycleBitmap(this.scaledBitmap);
		recycleBitmap(bitmap);
	}

	public boolean isTouched(int xPos, int yPos, Rect textBounds) {

		if (xPos < textBounds.right && xPos > textBounds.left && yPos < textBounds.bottom && yPos > textBounds.top) {
			return true;
		} else
			return false;

	}

	public String getName() {
		return info;
	}

	public void setInfo(String info) {
		this.info = info;
	}

	public int getInfoXPos() {
		if (infoXPos == 0) {
			return INITIAL_XPOS_NAME;
		} else
			return infoXPos;
	}

	public void setInfoXPos(int infoXPos) {
		this.infoXPos = infoXPos;
	}

	public int getInfoYPos() {
		if (infoYPos == 0) {
			return INITIAL_YPOS_NAME;
		} else
			return infoYPos;
	}

	public void setInfoYPos(int infoYPos) {
		this.infoYPos = infoYPos;
	}

	public Bitmap getBitmap() {
		return outputBitmap;
	}

	public Rect getInfoRect() {

		if (infoRect == null) {
			infoRect = new Rect();
		}

		infoTextPaint.getTextBounds(getName(), 0, getName().length(), infoRect);

		infoTextHeight = Math.abs(Math.abs(infoRect.bottom) - Math.abs(infoRect.top));
		infoTextWidth = Math.abs(Math.abs(infoRect.right) - Math.abs(infoRect.left));

		infoRect.top = getInfoYPos() - infoTextHeight;
		infoRect.bottom = (int) (getInfoYPos() + infoTextPaint.getTextSize());
		infoRect.left = getInfoXPos();
		infoRect.right = getInfoXPos() + infoTextWidth;

		return infoRect;

	}

	public void setInfoRect(Rect infoRect) {
		this.infoRect = infoRect;
	}
}
